<template>
	<view class="main">
		<u-navbar :is-back="true" title="用户登录" :background="background" title-size="32" title-color="#000"  :border-bottom="false" ></u-navbar>
		<view class="logo">
			<image src="../../../static/images/kong.png" mode=""></image>
		</view>
		<view class="input">
			<view class="input_l">
				<image src="../../../static/images/phone.png" mode="aspectFit"></image>
				<input type="text" maxlength="11" placeholder="请输入您的手机号" placeholder-class="ploc" v-model="phone" />
			</view>
			<view class="input_l" v-if="!logintype">
				<image src="../../../static/images/suo.png" mode="aspectFit"></image>
				<input type="password" maxlength="16" placeholder="请输入密码" placeholder-class="ploc" v-model="password" />
			</view>
			<view class="sendcode" v-if="logintype">
				<view class="input_r">
					<image src="../../../static/images/suo.png" mode="aspectFit"></image>
					<input type="text" maxlength="11" placeholder="请输入验证码" placeholder-class="ploc" v-model="code" />
				</view>
				<view class="code" @click="getCode">
					{{tips}}
				</view>
			</view>
		</view>
		<view class="bottom">
			<text v-if="!logintype" @click="changelogin(1)">手机号验证登录>></text>
			<text v-if="logintype" @click="changelogin(2)">密码登录></text>
			<text @click="router('../../register/register')">账号注册></text>
		</view>
		<view class="button" @click="handlogin">
			登 陆
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange">

		</u-verification-code>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				phone: '',
				password: '',
				background: {
					backgroundColor: '#fff',
				},
				tips: '',
				// refCode: null,
				seconds: 10,
				logintype: false,
				code: '',

			};
		},
		onLoad() {

		},
		methods: {
			//切换登陆类型
			changelogin(index) {
				if (index == 1) {
					this.logintype = true
				} else {
					this.logintype = false
				}
			},
			router(path) {
				uni.navigateTo({
					url: path
				})
			},

			//验证码倒计时
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.$u.toast('倒计时结束');
			},
			start() {
				this.$u.toast('倒计时开始');
			},

			Code() {
				// 获取code拼接到路径上
				var local = encodeURIComponent('http://wuyejf.app12345.cn/h5/'); // 当前H5页面的url
				var appid = 'wxf3eadd5e4a0da254';
				window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
					'&redirect_uri=' + local + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
			},


			//登陆
			handlogin() {
			
				if (!this.logintype) {
					if (this.phone != '' && this.password != '') {
						global.$http.request({
							url: '/login',
							data: {
								username: this.phone,
								password: this.password,
							}
						}).then(res => {
							console.log(res);
							console.log(res.data.code);
							if (res.data.code == 200) {
								uni.showToast({
									title: res.data.msg,
									icon: "none",
									mask: true,
									duration: 1000
								})
								console.log('登陆令牌', res.data.token);
								uni.setStorageSync('token', 'Bearer ' + res.data.token);
									this.Code()
								var that = this
								// setTimeout(function() {
								// 	console.log(222);
								// 	// uni.redirectTo({
								// 	// 	url: '../../indexs/index/index',
								// 	// })

								// }, 1000)

							}
						})
					} else {
						uni.showToast({
							title: '账号密码不能为空',
							icon: "none",
							mask: true,
							duration: 1000
						})
					}
				} else {
					if (this.logintype && this.phone != '' && this.code != '') {
						global.$http.request({
							url: '/loginByTelephone',
							data: {
								telephone: this.phone,
							}
						}).then(res => {
							console.log(res);
							console.log(res.data.code);
							if (res.data.code == 200) {
								uni.showToast({
									title: res.data.msg,
									icon: "none",
									mask: true,
									duration: 1000
								})
								uni.setStorageSync('token', 'Bearer ' + res.data.token);
								setTimeout(function() {
									uni.redirectTo({
										url: '../../indexs/index/index',
									})
								}, 1000)
							}
						})
					} else {
						uni.showToast({
							title: '账号和验证码不能为空',
							icon: "none",
							mask: true,
							duration: 1000
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #074D93;

		.main {
			width: 750rpx;
			height: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;

			.logo {
				margin-top: 118rpx;
				width: 309rpx;

				image {
					width: 309rpx;
					height: 265rpx;
				}
			}

			.input {
				margin-top: 45rpx;
				width: 550rpx;

				.sendcode {
					display: flex;
					align-items: center;
					border-bottom: 2rpx solid #fff;

					.input_r {
						font-weight: 400;
						color: #FFFFFF;
						line-height: 40rpx;
						display: flex;
						align-items: center;
						padding: 40rpx 15rpx;

						image {
							width: 24rpx;
							height: 29rpx;
						}

						input {
							margin-left: 29rpx;

							.ploc {
								color: #fff !important;
							}
						}
					}

					.code {
						margin-top: 20rpx;
						width: 140rpx;
						height: 48rpx;
						text-align: center;
						background: #ABE7FF;
						border-radius: 24rpx;
						font-size: 20rpx;
						font-weight: 400;
						color: #074D93;
						line-height: 40rpx;
					}
				}

				.input_l {
					font-weight: 400;
					color: #FFFFFF;
					line-height: 40rpx;
					display: flex;
					align-items: center;
					border-bottom: 2rpx solid #fff;
					padding: 40rpx 15rpx;

					image {
						width: 24rpx;
						height: 29rpx;
					}

					input {
						margin-left: 29rpx;

						.ploc {
							color: #fff !important;
						}
					}

				}
			}

			.bottom {
				margin: 35rpx 0;
				width: 550rpx;
				display: flex;
				justify-content: space-between;

				text {
					font-size: 26rpx;
					font-weight: 400;
					color: #ABE7FF;
				}
			}

			.button {
				margin-top: 126rpx;
				width: 550rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				background: #ABE7FF;
				border-radius: 40rpx;

				font-size: 32rpx;
				font-weight: bold;
				color: #074D93;
			}
		}

	}
</style>
